package com.fish.anne.webapp.action;

import java.util.List;

import com.fish.anne.model.Auto;
import com.fish.anne.model.Member;
import com.fish.anne.model.Order;
import com.fish.anne.model.OrderLog;
import com.fish.anne.service.AutoManager;
import com.fish.anne.service.MemberManager;
import com.fish.anne.service.OrderLogManager;
import com.fish.anne.service.OrderManager;
import com.fish.anne.webapp.formbean.OrderSearch;
import com.opensymphony.xwork2.Preparable;

public class OrderAction extends BaseAction implements Preparable {
    private OrderManager orderManager;
    
    private MemberManager memberManager;
    private Member member;
    private AutoManager autoManager;
    private Auto auto ;
    
    private OrderLogManager orderLogManager;
    public void setOrderLogManager(OrderLogManager orderLogManager) {
		this.orderLogManager = orderLogManager;
	}

	public void setAuto(Auto auto) {
		this.auto = auto;
	}

	public void setAutoManager(AutoManager autoManager) {
		this.autoManager = autoManager;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void setMemberManager(MemberManager memberManager) {
		this.memberManager = memberManager;
	}

	private List<Order> orders;
    private Order order;
    private Long  orderId;

    public void setOrderManager(OrderManager orderManager) {
        this.orderManager = orderManager;
    }

    public List<Order> getOrders() {
        return orders;
    }

    private OrderSearch orderSearch;
    public OrderSearch getOrderSearch() {
		return orderSearch;
	}

	public void setOrderSearch(OrderSearch orderSearch) {
		this.orderSearch = orderSearch;
	}

	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String orderId = getRequest().getParameter("order.orderId");
            if (orderId != null && !orderId.equals("")) {
                order = orderManager.get(new Long(orderId));
            }
        }
    }

    public String stat(){
    	return SUCCESS;
    }
    
    public String list() {
    	if(orderSearch==null){
    		orderSearch = new OrderSearch();
		}
		Long count = orderManager.getCountBySearch(orderSearch);
		this.orderSearch.setPageTotalRow(count);		
 		orderSearch.setPageCurrentPage(getPageCurrentPage());
		orders = orderManager.find(orderSearch);    	
         
        return SUCCESS;
    }

    public void setOrderId(Long  orderId) {
        this. orderId =  orderId;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public String delete() {
        orderManager.remove(orderId);
        saveMessage(getText("order.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (orderId != null) {
            order = orderManager.get(orderId);
            member = this.memberManager.get(order.getMemberId());
            auto = this.autoManager.get(order.getAutoId());
        }  

        return SUCCESS;
    }

    public Auto getAuto() {
		return auto;
	}

	/**
     * 退款  /
     * @return
     * @throws Exception
     */
    public String save() throws Exception {
        if(order.getOrderId() == null){
        	return "";
        }
        this.orderId = order.getOrderId() ;
        Order old = this.orderManager.get(orderId);
        
        
        
        //step one
        orderManager.updateStatus(order);  
        
      //step one
        {
        	OrderLog orderLog = new OrderLog();
            orderLog.setBehavior("change_status" );        
            orderLog.setCreateTime(System.currentTimeMillis());
            orderLog.setLogText("change status from\t"+old.getStatus()+"\tto\t"+order.getStatus());
            orderLog.setOpId(this.getAdminUserId());
            orderLog.setOpName(this.getSystemAdminUser().getName());
            orderLog.setOpType((byte)0);
            orderLog.setOrderId(orderId);
            orderLog.setResult((byte)0);
            this.orderLogManager.save(orderLog);
        }
        
        
        
        
        saveMessage("修改完成");
        return SUCCESS;
         
    }

	public Long getOrderId() {
		return orderId;
	}
}